// pages/home/submit/submit.js
const app=getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    current: 1,
    tables: [
      "fuel_discharge",
      "garbage_discharge",
      "iron_discharge",
      "outsourced_stream_discharge",
      "refrigeration_equipment_discharge",
    ],
    tablesName: ["燃料排放", "垃圾处理", "钢铁原料", "外购电力", "制冷设备"],
    attributes: {
      fuel_discharge:[
        "自产煤",
        "原料煤",
        "燃料煤",
        "无烟煤",
        "焦煤",
        "烟煤",
        "次烟煤",
        "石油焦",
        "原油",
        "煤油",
        "页岩油",
        "乙烷",
        "天然气",
        "炼油气",
        "炼焦炉气",
        "高炉气",
        "黑液",
        "木炭",
        "其他固体燃料",
        "生质汽油",
        "生质柴油",
      ],
      garbage_discharge:["机械式焚化炉", "机械式焚化炉", "掩埋设施"],
      iron_discharge:[
        "外购粒状燃料",
        "预焙阳极炭块",
        "聚氯乙烯PVC",
        "C10H8O4",
        "聚乙烯PE",
        "白云石",
        "石灰石",
        "原料-铁",
        "原料-钢",
        "焦炉煤气",
        "高炉煤气",
        "转炉煤气",
        "残余燃料油",
        "焦油",
      ],
      outsourced_stream_discharge:[
        "燃料煤",
        "燃料油",
        "天然气(NG)",
        "液化天然气",
        "液化石油气",
        "液化石油气",
      ],
      refrigeration_equipment_discharge:[
        "独立商用冷冻、冷藏装备",
        "中、大型冷冻、冷藏装备",
        "交通用冷冻、冷藏装备",
      ],
    },
    attributes_id: {
      fuel_discharge:[
        1,
        2,
        3,
        4,
        5,
        6,
        7,
        13,
        16,
        19,
        20,
        28,
        29,
        30,
        31,
        32,
        37,
        38,
        29,
        40,
        41,
      ],
      garbage_discharge:[1, 2, 3],
      iron_discharge:[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14],
      outsourced_stream_discharge:[1, 2, 3, 4, 5, 6],
      refrigeration_equipment_discharge:[2, 3, 4],
    },
    array: new Array(10),
    sms :'123',
    id:'',
    value:[],
    name:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const tables=this.data.tables
    const current=this.data.current
    

    const that=this
    wx.request({
      url: `http://localhost:8080/user/getMyMsg?WXID=${app.WXID}`,
      header: {
        'content-type': 'application/json' // 默认值
      }, 
      success (res) {
        // console.log("user",res.data[0])
        let user=res.data[0]
        // console.log(user)
        const {id}=user
        that.setData({id})
        console.log(id)
      }
    })
  },

  handleChange(e) {
    const current=this.data.current
    const type = e.currentTarget.dataset.dire;
    if (type === "next") {
      let p=''
      if(current ==1 ){
        p='fuel'
      }else if(current ==2 ){p='garbage'}
      else if(current == 3){p='iron'}
      else if(current == 4){p='outsourcedStream'}
      else if(current == 5){p='refrigerationEquipment'}
      wx.request({
        url: `http://localhost:8080/Receive/${p}`,
        method:'POST',
        data:{
          company_id:this.data.id,
          type:this.data.name,
          discharge:this.data.value
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded' // 默认值
        }, 
        success (res) {
          console.log(res)
          if(current ==5){
            wx.redirectTo({
              url: '../../home/home',
            })
          }
        },
      })
      if(current !=5){
        this.setData({
          current: this.data.current + 1,
        });
      }
      
      this.setData({value:[],name:[]})
    } else if (type === "prev") {
      this.setData({
        current: this.data.current - 1,
      });
    }
  },

  changeV(e){
    let val=e.detail.value
    let name=e.currentTarget.dataset.name
    
    let values=[]
    values=[...this.data.value,val]
    values.forEach((item,index) =>{
	    values[index] = parseInt(values[index])
    })
    
    let names=[]
    names=[...this.data.name,name]
    this.setData({value:values,name:names})
    // console.log(values,names)
  }
});
